package com.J.Plane;

import static android.opengl.GLES10.*;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.Log;

public class PlaneRenderer implements GLSurfaceView.Renderer {

	private int[] textures = new int[3];
	private Context mContext;
	private SceneNode nodeBigImage;
	private SceneNode nodeSmallImage;

	private SceneManager mSceneMgr;

	/** Brian - Input by touch event */
	private float _xAngle;
	private float _yAngle;

	public void setXAngle(float angle) {
		_xAngle = angle;
	}

	public float getXAngle() {
		return _xAngle;
	}

	public void setYAngle(float angle) {
		_yAngle = angle;
	}

	public float getYAngle() {
		return _yAngle;
	}

	public PlaneRenderer(Context context) {
		mContext = context;
	}

	public PlaneRenderer() {

	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glDisable(GL_DITHER);
		gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);

		gl.glClearColor(.5f, .5f, .5f, 1);
		gl.glShadeModel(GL_SMOOTH);
		gl.glEnable(GL_DEPTH_TEST);
		gl.glEnable(GL_TEXTURE_2D);
		//GLU.gluLookAt(gl, 0,0,0,0,0,0,0,0,0);
		
		
		BitmapMaterial mat = new BitmapMaterial(gl, mContext.getResources(),
				R.drawable.sohn);
		BitmapMaterial mat1 = new BitmapMaterial(gl, mContext.getResources(),
				R.drawable.icon);
		BitmapMaterial mat2 = new BitmapMaterial(gl, mContext.getResources(),
				R.drawable.robot);
		
		Plane plane1 = new Plane(4, 4, 4, 4, mat);
		Plane plane2 = new Plane(4, 4, 4, 4, mat1);
		
		
		/**
		 * 
		 * 1. Define SceneNode instance
		 * 		a. translate instance in terms of PARENT instance
		 * 		b. rotate instance in terms of PARENT instance
		 * 2. Create nodeBridge instance
		 * 		a. create bridge instance scene node.
		 * 		b. translate the bridge as you want to coordinate
		 * 		c. addChild, sceneNode, as you want to connect 
		 * 
		 * nodeBridge <- nodeBigImage <- nodeBridge1 <- nodeSmallImage1 <- nodeBridge2 <- nodeSmallImage2
		 * 
		 */
		
		
		SceneNode nodeBigImage = new SceneNode(plane1).name("big").translate(2, 0, 0).rotate(_xAngle, 0, 1, 0);
		SceneNode nodeBigImage1 = new SceneNode(plane1).name("big1").translate(-2, 0, 0).rotate(_xAngle, 0, 1, 0);
		SceneNode nodeBigImage2 = new SceneNode(plane2).name("big2").translate(2, 0, 0).rotate(_xAngle, 0, 1, 0);
		
		SceneNode nodeSmallImage1 = new SceneNode(plane2).scale(0.5f).translate(0, 0, 1).name("small").rotate(90, 1, 0, 0);
		SceneNode nodeSmallImage2 = new SceneNode(plane2).scale(.5f).translate(0, 1, 0).name("small2").rotate(0, 1, 0, 0);
		SceneNode nodeSmallImage3 = new SceneNode(plane2).scale(.5f).translate(-1, 0, 0).name("small3").rotate(0, 0, 1, 0);
		
		SceneNode nodeSmallImage4 = new SceneNode(plane2).scale(.5f).translate(0, 1, 0).name("small3").rotate(0, 0, 1, 0);
		
		//nodeBridge <- nodeBigImage
		SceneNode nodeBridge = new SceneNode("middle").translate(0, 0, 0);
		nodeBridge.addChild(nodeBigImage);
		
		//nodeBridge2 <- nodeSmallImage2
		SceneNode nodeBridge2 = new SceneNode("middle2").translate(0,2,0);
		nodeBridge2.addChild(nodeSmallImage2);

		//nodeSmallImage1 <- nodeBridge2
		nodeSmallImage1.addChild(nodeBridge2);		
		
		//nodeBridge1 <- nodeSmallImage1
		SceneNode nodeBridge1 = new SceneNode("middle1").translate(0, 2, 0);
		nodeBridge1.addChild(nodeSmallImage1);
		
		// nodeBigImage <- nodeBridge1
		nodeBigImage.addChild(nodeBridge1);

		//nodeBridge3 <- nodeSmallIMage3
		SceneNode nodeBridge3 = new SceneNode("middle3").translate(-2,0,0);
		nodeBridge3.addChild(nodeSmallImage3);
		
		nodeBigImage1.addChild(nodeBridge3);
		
		//nodeBridgeStatic <- nodeBigImage2
		SceneNode nodeBridgeStatic = new SceneNode("static").translate(0, 0, 0);
		nodeBridgeStatic.addChild(nodeBigImage2);
		
		SceneNode nodeBridge4 = new SceneNode("middle4").translate(1, 0, 0);
		nodeBridge4.addChild(nodeSmallImage4);
		nodeBigImage2.addChild(nodeBridge4);
		
		
		mSceneMgr = new SceneManager();
		mSceneMgr.addNode(nodeBridge);
		mSceneMgr.addNode(nodeBridgeStatic);
		
		mSceneMgr.addNode(nodeBigImage1);
		
		mSceneMgr.camera(new Camera(Camera.GLOBAL_CAMERA));
		mSceneMgr.camera().eye(0, 0, 10).at(0, 0, -1).up(0, 1, 0);
		
	}

	public void onSurfaceChanged(GL10 gl, int w, int h) {
		mSceneMgr.camera().fovy(5).near(3).far(20).viewport(gl, w, h);
	}

	public void onDrawFrame(GL10 gl) {
		gl.glDisable(GL_DITHER);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		
		mSceneMgr.camera().setProjectionMatrix(gl);
		mSceneMgr.camera().setViewToWorldMatrix(gl);
		mSceneMgr.camera().eye(0, -5, 10).at(0, -10, 0).up(0, 1, 0);
		
		if (_xAngle < 90){
			if (_xAngle > -90){
				mSceneMgr.node("middle4").rotate((float) (_xAngle), 1, 0, 0);
				mSceneMgr.node("middle3").rotate((float) (_xAngle*2), 0, 1, 0);
				mSceneMgr.node("middle2").rotate((float) (-_xAngle*1.8), 1, 0, 0);
				mSceneMgr.node("middle1").rotate((float) (_xAngle*.8), 1, 0, 0);
				mSceneMgr.node("middle").rotate(-_xAngle, 0, 1, 0);
				mSceneMgr.renderScene(gl);
			}
			else{
				mSceneMgr.node("middle3").rotate((float) (-_xAngle*2), 0, 1, 0);
				mSceneMgr.node("middle3").rotate((float) (_xAngle*2), 0, 1, 0);
				mSceneMgr.node("middle2").rotate((float) (-_xAngle*1.8), 1, 0, 0);
				mSceneMgr.node("middle1").rotate((float) (_xAngle*.8), 1, 0, 0);
				mSceneMgr.node("middle").rotate(-_xAngle, 0, 1, 0);
				mSceneMgr.renderScene(gl);
			}
		}
		else{			
				mSceneMgr.node("middle2").rotate((float) (90.0), 1, 0, 0);
				mSceneMgr.node("middle1").rotate((float) (90.0), 1, 0, 0);
				mSceneMgr.node("middle").rotate(-_xAngle, 0, 1, 0);
				mSceneMgr.renderScene(gl);
		}
	
		
	}

}
